package org.jrazdacha.bittorrent.modelling.core;


/**
 * Represents connection information between two peers
 * 
 * @author nep
 *
 */
public class Connection {

	private ConnectionState connectionState = ConnectionState.DISCONNECTED;

	public ConnectionState getState() {
		return connectionState;
	}

	public void handshakeWasSend() {
		if (ConnectionState.DISCONNECTED.equals(connectionState)) {
			connectionState = ConnectionState.HANDSHAKE_SENDED;
		} else if (ConnectionState.RECEIVED_HANDSHAKE.equals(connectionState)) {
			connectionState = ConnectionState.REPLY_HANDSHAKE_SENDED;
		}
	}

	public void bitfieldWasSend() {
		if (ConnectionState.HANDSHAKE_REPLY_ARRIVED.equals(connectionState)) {
			connectionState = ConnectionState.BITFIELD_SEND;
		} else if (ConnectionState.BITFIELD_RECEIVED.equals(connectionState)) {
			connectionState = ConnectionState.ESTABLISHED;
		}
	}

	public void handshakeReceived() {
		if (ConnectionState.DISCONNECTED.equals(connectionState)) {
			connectionState = ConnectionState.RECEIVED_HANDSHAKE;
		} else if (ConnectionState.HANDSHAKE_SENDED.equals(connectionState)) {
			connectionState = ConnectionState.HANDSHAKE_REPLY_ARRIVED;
		}
	}

	public void bitfieldReceived() {
		if (ConnectionState.REPLY_HANDSHAKE_SENDED.equals(connectionState)
				|| ConnectionState.HANDSHAKE_REPLY_ARRIVED.equals(connectionState)) {
			connectionState = ConnectionState.BITFIELD_RECEIVED;
		} else if (ConnectionState.BITFIELD_SEND.equals(connectionState)) {
			connectionState = ConnectionState.ESTABLISHED;
		} 
	}
}
